import { createRouter, createWebHashHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView,
      meta: {
        title: '首页',
        show: true,
      },
    },
    {
      path: '/about',
      name: 'about',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../views/AboutView.vue'),
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/LoginView.vue'),
    },
    {
      path: '/reg',
      name: 'reg',
      component: () => import('../views/RegView.vue'),
    },
    {
      path: '/me',
      name: 'me',
      component: () => import('../views/MeView.vue'),
      meta:{
        requireLogin:true
      }
    },
    {
      path: '/detail',
      name: 'detail',
      component: () => import('../views/DetailView/IndexView.vue'),
      meta: {
        title: '详情页',
        show: false,
      },
    },
    {
      path: '/cart',
      name: 'cart',
      component: () => import('../views/CartView.vue'),
      meta:{
        requireLogin:true,
        show:true,
      }
    },
    {
      path: '/search',
      name: 'search',
      component: () => import('../views/SearchView.vue'),
      meta:{
        show:true,
      }
    },
  ],
});

router.beforeEach((to)=>{
  if(to.meta.requireLogin==true){
    let token=localStorage.getItem('mallFrontToken');
    if (token==null) {
      alert('请先登录');
      router.push('/login');
    }
  }
})

export default router
